package com.example.yuejian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuejian.Bean.Post;
import com.example.yuejian.R;
import com.example.yuejian.UnitData;
import com.example.yuejian.activity.PostDetail;
import com.squareup.picasso.Picasso;

import java.util.List;


public class MyPostAdapter extends  RecyclerView.Adapter< RecyclerView.ViewHolder> {
    private Context mContext;
    private List< Post.PostlistBean> mList;
    public LayoutInflater mLayoutInflater;

    public MyPostAdapter(Context mContext, List< Post.PostlistBean> List) {
        this.mContext = mContext;
        this.mList = List;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_post_post, parent, false);
        MyPostAdapter.ViewHolder v = new MyPostAdapter.ViewHolder(view);

        return v;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Post.PostlistBean club = ( Post.PostlistBean) mList.get(position);
        if (null == club)
            return;
        MyPostAdapter.ViewHolder viewHolder = (MyPostAdapter.ViewHolder) holder;
        viewHolder.name.setText(club.getUsername());
        viewHolder.data.setText(club.getDate());
        viewHolder.number.setText(club.getContent());
        if(!club.getPic().isEmpty()) {
            Picasso.with(mContext).load(UnitData.IMG_URL+club.getPic()).into(viewHolder.img_club);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(mContext, PostDetail.class);
                bundle.putString("pic",UnitData.IMG_URL+club.getPic());
                bundle.putString("intro",club.getContent());
                bundle.putString("time",club.getDate());
                bundle.putString("userid",club.getUserid());
                bundle.putString("postid",club.getPostid());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name,data,number;
        ImageView img_club;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            data = (TextView) itemView.findViewById(R.id.data);
            number = (TextView) itemView.findViewById(R.id.number);
            img_club = (ImageView) itemView.findViewById(R.id.img_club);

        }
    }
}
